﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Word.Editor.Doc
{
    /// <summary>
    /// The PlcBteChpx structure is a PLC that maps the offsets of text in the WordDocument stream to the character properties of that text. 
    /// Where most PLCs map CPs to data, the PlcBteChpx maps stream offsets to data instead. 
    /// A PlcBteChpx MUST NOT contain duplicate stream offsets.
    /// </summary>
    public class PlcBteChpx:PLC
    {
        public PlcBteChpx():base()
        { }
        public int DataNo = 0;
        /// <summary>
        /// An array of unsigned integers. Each element in this array specifies an offset in the WordDocument stream where text begins. 
        /// The end of each range is the beginning of the next range. As with all PLCs, the elements of aFC MUST be sorted in ascending order.
        /// </summary>
        public UInt32[] aFC = null;
        /// <summary>
        /// An array of PnFkpChpx (4 bytes each). 
        /// Each element of this array specifies the location in the WordDocument stream of a ChpxFkp. 
        /// That ChpxFkp contains the character properties for the text at the corresponding offset in aFC.
        /// </summary>
        public UInt32[] aPnBteChpx = null;

        public override void ParseBuffer(byte[] buffer)
        {
            base.ParseBuffer(buffer);
            DataNo = (buffer.Length - 4) / 8;
            aFC = new UInt32[DataNo + 1];
            if (DataNo == 0)
                throw new IndexOutOfRangeException(Strings.DataArraySize_InvalidValue);
            aPnBteChpx = new UInt32[DataNo];

            for (int i = 0; i < DataNo + 1; i++)
            {
                aFC[i] = GetUint32(buffer, i * 4);
            }
            for (int i = 0; i < DataNo; i++)
            {
                int st =(DataNo + 1) * 4 + i * 4;
                aPnBteChpx[i] = GetUint32(buffer, st);
                aPnBteChpx[i] = (aPnBteChpx[i] << 10) >> 10;
            }
        }

        public override String ToString()
        {
            StringBuilder ret = new StringBuilder();
            for (int i = 0; i < DataNo; i++)
            {
                ret.AppendFormat("\nFC[{0}]={1:X}-FC[{2}]={3:X}", i, aFC[i], i + 1, aFC[i + 1]);
                ret.AppendFormat("\naPnBteChpx[{0}]={1:X}", i, aPnBteChpx[i]);
            }
            return ret.ToString();
        }

        internal void AddToTree(System.Windows.Forms.TreeNode root)
        {
            TreeNode aCPTN = root.Nodes.Add("aFC");
            for (int i = 0; i < DataNo + 1; i++)
            {
                aCPTN.Nodes.Add("aFC" + i.ToString(), string.Format("{0}={1:x}", i, aFC[i]));
            }
            TreeNode aPcdTN = root.Nodes.Add("aPnBteChpx");
            for (uint i = 0; i < DataNo; i++)
            {
                aPcdTN.Nodes.Add("aPnBteChpx" + i.ToString(), string.Format("{0}={1:x}", i, aPnBteChpx[i]));
            }
        }

    }
}
